﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.HVAC
{
	public class LayRadiatorDrawJinPolarAxis : XUiJigBase
	{
		public LayRadiatorDrawJinPolarAxis(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = LayRadiatorDrawJinPolarAxis.EStage.eNoOper;
			this.ResetPoint = XUiJigBase.EResetPoint.eByAxis;
		}

		public XYZ PickStartPoint(XYZ sp, string strPrompt = "请确定直线起点：")
		{
			try
			{
				this.JigStage = LayRadiatorDrawJinPolarAxis.EStage.eStartPt;
				this.StartPoint = sp;
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = LayRadiatorDrawJinPolarAxis.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickEndPoint(XYZ ptStart, string strPrompt = "请确定直线终点：")
		{
			try
			{
				this.JigStage = LayRadiatorDrawJinPolarAxis.EStage.eEndPt;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.EndPoint = this.GetResetPoint(this.StartPoint, this.EndPoint);
			}
			catch
			{
				if (this.m_prompDlg != null && base.CmdEndType == XUiJigBase.ECmdEndType.eEnter)
				{
					base.CmdEndType = XUiJigBase.ECmdEndType.eOther;
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					xyz = this.GetResetPoint(this.StartPoint, xyz);
					double num = Convert.ToDouble(this.m_prompDlg.GetInputValue()) / 304.8;
					XYZ xyz2 = (xyz - this.StartPoint).Normalize();
					this.EndPoint = this.StartPoint + xyz2 * num;
				}
				else
				{
					this.EndPoint = null;
				}
			}
			this.JigStage = LayRadiatorDrawJinPolarAxis.EStage.eNoOper;
			return this.EndPoint;
		}

		public XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (this.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
			{
				result = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, ptBase, this.LineAngle);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
			}
			else if (this.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
			{
				XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, ptBase);
				if (axisPoint != null)
				{
					result = axisPoint;
				}
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && paintEventArgs != null && this.JigStage == LayRadiatorDrawJinPolarAxis.EStage.eEndPt)
			{
				XYZ ptUser = base.Client2Revit(this.m_ptCur);
				if (this.ResetPoint == XUiJigBase.EResetPoint.eByAngle)
				{
					XYZ projectPoint = JigFuncs.GetProjectPoint(base.UiDoc, ptUser, this.StartPoint, this.LineAngle);
					this.m_ptCur = base.Revit2Client(projectPoint);
				}
				else if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
				{
					XYZ orthoPoint = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, this.StartPoint);
					this.m_ptCur = base.Revit2Client(orthoPoint);
				}
				else if (this.ResetPoint == XUiJigBase.EResetPoint.eByAxis)
				{
					XYZ axisPoint = JigFuncs.GetAxisPoint(base.UiDoc, ptUser, this.StartPoint);
					if (axisPoint != null)
					{
						this.m_ptCur = base.Revit2Client(axisPoint);
						base.DrawAxisLine(paintEventArgs.Graphics, this.StartPoint, axisPoint);
					}
				}
				XYZ currentPoint = base.Client2Revit(this.m_ptCur);
				this.resultLines = LayRadiatorDrawUtility.Draw(base.Doc.ActiveView, this.StartPoint, currentPoint, false);
				XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
				foreach (Line curve in this.resultLines)
				{
					new XLine2D(base.Revit2Client(curve.GetEndPoint(0)), base.Revit2Client(curve.GetEndPoint(1))).Draw(g);
				}
			}
		}

		private System.Drawing.Point Revit2Client()
		{
			throw new NotImplementedException();
		}

		public double LineAngle { get; set; }

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public LayRadiatorDrawJinPolarAxis.EStage JigStage { get; set; }

		public XUiJigBase.EResetPoint ResetPoint { get; set; }

		public List<Line> resultLines = new List<Line>();

		public enum EStage
		{
			eNoOper,
			eStartPt,
			eEndPt
		}
	}
}
